package com.fg.cloud.wechar.controller.system;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.finals.ProblemStatus;
import com.fg.cloud.common.finals.UserType;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.manager.entry.system.ProblemInteractiveEntity;
import com.fg.cloud.manager.entry.system.SystemProblemEntity;
import com.fg.cloud.manager.entry.system.vo.SystemProWecharVo;
import com.fg.cloud.manager.entry.system.vo.SystemProblemVo;
import com.fg.cloud.manager.service.agency.AgencyService;
import com.fg.cloud.manager.service.system.SystemProblemService;
import com.fg.cloud.wechar.controller.BaseWeCharController;

/**
 * Description:经销商手机端问题反馈
 * @author dingjiang
 * @date 2017年9月6日上午10:08:02
 */
@Controller
@RequestMapping("/systemProWeChar")
public class SystemProblemWechar extends BaseWeCharController {

	@Autowired
	private SystemProblemService systemProblemService;
	
	@Autowired
	private AgencyService agencyService;
	
	
	/**
	 * Description:经销商手机端问题反馈
	 * @param req
	 * @param resp
	 * @param systemProblemEntity
	 * @author dingjiang
	 * @date 2017年8月23日上午11:34:29
	 */
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping(value="/insert",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void insert(HttpServletRequest req, HttpServletResponse resp, SystemProblemEntity systemProblemEntity){
		try {
			//验证参数非空
			if (null == systemProblemEntity.getTitle() || StringUtils.isEmpty(systemProblemEntity.getTitle())) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "标题不能为空");
				return ;
			}
			if (null == systemProblemEntity.getContent() || StringUtils.isEmpty(systemProblemEntity.getContent())) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "内容不能为空");
				return ;
			}
			//判重
			systemProblemEntity.setFacId(getCurrentAgencyUser().getFacId());
			systemProblemEntity.setAgeId(getCurrentAgencyUser().getAgeId());
			if ( systemProblemService.selectBycondition(systemProblemEntity) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "问题记录已存在");
				return ;
			}
			systemProblemEntity.setFeederName(agencyService.find(getCurrentAgencyUser().getAgeId()).getAgeName());
			systemProblemEntity.setPhone(getCurrentAgencyUser().getPhone());
			systemProblemEntity.setCreator(getCurrentAgencyUser().getId());
			systemProblemEntity.setUpdater(getCurrentAgencyUser().getId());
			systemProblemService.insertSelective(systemProblemEntity);
			doResult(resp, true, StatusCode.SAVESUCCESS, null, ResultType.SAVE_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.SAVEERROR, null, e.getMessage());
		}
	}
	
	/**
	 * Description:经销商手机端分页查看反馈记录
	 * @param req
	 * @param resp
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @param type
	 * @param status
	 * @author dingjiang
	 * @date 2017年9月5日上午11:38:26
	 */
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping(value="/selectpage",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void selectPage(HttpServletRequest req, HttpServletResponse resp, Integer page, Integer rows, 
			String sort, String order, Byte type, Byte status ){
		try {
			if (null == page) {
				page = 1;
			}
			if (null == rows) {
				rows = 10;
			}
			PageUtils<SystemProblemVo> pageinfo = new PageUtils<SystemProblemVo>(page, rows, sort, order);
			pageinfo.setSort("status");
			pageinfo.setOrder("desc");
			pageinfo.getCondition().put("ageId = ", getCurrentAgencyUser().getAgeId());
			pageinfo.getCondition().put("facId = ", getCurrentAgencyUser().getFacId());
			if ( type != null ) {
				pageinfo.getCondition().put("type = ", type );
			}
			if ( status != null ) {
				pageinfo.getCondition().put("status = ", status );
			}
			systemProblemService.selectWechar(pageinfo);
			doResult(resp, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.FINDERROR, null, e.getMessage());
		}
	}
	
	/**
	 * Description:经销商手机端查看问题详情以及处理结果
	 * @param req
	 * @param resp
	 * @param id
	 * @author dingjiang
	 * @date 2017年9月5日下午1:38:22
	 */
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping(value="/viewDetails",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void viewDetails(HttpServletRequest req, HttpServletResponse resp,  Long id ){
		try {
			//验证非空
			if (LongUtils.isEmpty(id)) {
				doResult(resp, false, StatusCode.FINDERROR, null, ResultType.INPUT_NULL);
				return ;
			}
			//判断输入id是否合法
			SystemProWecharVo findVo = systemProblemService.wfind(id, getCurrentAgencyUser().getAgeId());
			if ( null == findVo ) {
				doResult(resp, false, StatusCode.FINDERROR, null, ResultType.INPUT_ERROR);
				return ;
			}
			findVo.setResult( findVo.getProblemInteractiveEntities().get(0).getContent() );
			doResult(resp, true, StatusCode.FINDSUCCESS, findVo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.FINDERROR, null, e.getMessage());
		}
	}
	
	/**
	 * Description:经销商手机端问题补充
	 * @param req
	 * @param resp
	 * @param id
	 * @param text
	 * @author dingjiang
	 * @date 2017年9月5日下午2:37:53
	 */
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping(value="/replenish",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void replenish(HttpServletRequest req, HttpServletResponse resp, Long id, String text){
		try {
			//验证反馈ID非空
			if (LongUtils.isEmpty(id)) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "请选择问题");
				return ;
			}
			SystemProblemEntity systemProblemEntity = systemProblemService.find(id, getCurrentAgencyUser().getAgeId());
			//验证反馈id是否合法
			if ( null == systemProblemEntity ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.INPUT_ERROR);
				return ;
			}
			//判断问题状态
			if ( systemProblemEntity.getStatus() == ProblemStatus.CLOSED ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "问题已关闭");
				return ;
			}
			//验证补充内容非空
			if (StringUtils.isEmpty(text)) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "请输入补充内容");
				return ;
			}
			//补充问题
			ProblemInteractiveEntity interactive = new ProblemInteractiveEntity();
			interactive.setType((byte) 2);
			interactive.setContent(text);
			interactive.setProId(id);
			interactive.setCreator(getCurrentAgencyUser().getId());
			interactive.setUpdater(getCurrentAgencyUser().getId());
			systemProblemService.reply(interactive);
			//修改问题状态
			systemProblemEntity.setStatus(ProblemStatus.PENDING);
			systemProblemEntity.setUpdater(getCurrentAgencyUser().getId());
			systemProblemService.updateByPrimaryKeySelective(systemProblemEntity);
			doResult(resp, true, StatusCode.SAVESUCCESS, null, ResultType.SAVE_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.SAVEERROR, null, e.getMessage());
		}
	}
	
	/**
	 * Description:经销商手机端修改问题
	 * @param req
	 * @param resp
	 * @param systemProblemEntity
	 * @author dingjiang
	 * @date 2017年9月5日下午1:56:52
	 */
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping(value="/modify",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void modify(HttpServletRequest req, HttpServletResponse resp, SystemProblemEntity systemProblemEntity){
		try {
			//验证参数非空
			if ( null == systemProblemEntity.getTitle() || StringUtils.isEmpty(systemProblemEntity.getTitle()) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "标题不能为空");
				return ;
			}
			if ( null == systemProblemEntity.getContent() || StringUtils.isEmpty(systemProblemEntity.getContent()) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "内容不能为空");
				return ;
			}
			//判断状态
			if ( systemProblemEntity.getStatus() == ProblemStatus.PROCESSED ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "问题已处理，无法修改");
				return ;
			}
			//判重
			if ( systemProblemService.selectBycondition(systemProblemEntity) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.INPUT_ERROR);
				return ;
			}
			systemProblemEntity.setUpdater(getCurrentAgencyUser().getId());
			systemProblemService.updateByPrimaryKeySelective(systemProblemEntity);
			doResult(resp, true, StatusCode.SAVESUCCESS, null, ResultType.SAVE_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.SAVEERROR, null, e.getMessage());
		}
	}
	
	
}
